function oos=wnom_out_of_sample_acc(session,dimension,sample,flag0)
%WNOM_OUT_OF_SAMPLE_ACC computes the out-of-sample accuracy for the holdout
%sample given by SAMPLE for Senate SESSION and the W-NOMINATE model with
%dimension given by DIMENSION.
%
%OUT is a 10 dimensional vector containing the out-of-sample predictions
%for each sample.

for j1=sample
    
    %Read in the data from the wnominate runs for each holdout sample
    if flag0==0
        leg=xlsread(['./results_',num2str(dimension),'d/senate-',num2str(session),'-',num2str(j1),'-leg-',num2str(dimension),'d.csv']);
        leg=leg(:,7:7+dimension-1);
        rc=xlsread(['./results_',num2str(dimension),'d/senate-',num2str(session),'-',num2str(j1),'-rollcall-',num2str(dimension),'d.csv']);
        rc=rc(:,8:8+2*dimension-1);
        b=load(['./holdout samples/senate-',num2str(session),'-holdout-',num2str(j1),'.mat']);
    else
        leg=xlsread(['./results_',num2str(dimension),'d/house-',num2str(session),'-',num2str(j1),'-leg-',num2str(dimension),'d.csv']);
        leg=leg(:,7:7+dimension-1);
        rc=xlsread(['./results_',num2str(dimension),'d/house-',num2str(session),'-',num2str(j1),'-rollcall-',num2str(dimension),'d.csv']);
        rc=rc(:,8:8+2*dimension-1);
        b=load(['./holdout samples/house-',num2str(session),'-holdout-',num2str(j1),'.mat']);
    end
    
    
    data=b.votes_no_holdout;
    
    %From a matrix where we hold that labels of the holdout sample
    compmat=zeros(size(data));
    compmat(b.hinds)=1;
    
    
    %If the first vote has been discarded, put a missing data placeholder back in
    flag=1;
    if size(rc,1)==size(data,2)-1
        rc=[nan(size(rc(1,:)));rc];flag=0;
    end
    %NOMINATE has removed the close votes, find them - they'll be imported as NaN
    binds=find(isnan(sum(rc')'));
    if flag==0
        binds=[1,binds];
    end
    if flag0==0
        save(['./holdout samples/senate-',num2str(session),'-dim-',num2str(dimension),'-bad-votes.mat'],'binds');
    else
        save(['./holdout samples/house-',num2str(session),'-dim-',num2str(dimension),'-bad-votes.mat'],'binds');
    end
    %Create a copy of the data and remove these votes
    data0=data;
    data0(:,binds)=[];
    %Remove them from the other matrices as well
    rc(binds,:)=[];
    compmat(:,binds)=[];
    %Record the remaining held out data
    hinds0=find(compmat==1);
    
    %Next, we run through the roll calls and use the coordinates to
    %estimate the votes of the legislators.
    
    Z=zeros(size(data0));Z1=Z;Z2=Z;
    for i=1:size(leg,1)
        for j=1:size(rc,1)
            %Construct cutting line
            tmp=rc(j,:);
            tmp1=leg(i,:);
            f=@(v)(tmp(1:dimension)*(v-tmp(dimension+1:end))');
            tst=f(tmp1);
            %After testing against the cutting line, construct
            %two response matrices (Z,Z1) with opposite votes
            %since we don't know which direction the groups are
            %voting.  Here we record yes votes by 1, no votes by 6,
            %abstentions by 9.
            if tst<0
                Z(i,j)=1;Z1(i,j)=-1;
            end
            if tst>0
                Z(i,j)=-1;Z1(i,j)=1;
            end
            %Keep the abstentions where they are
            if data0(i,j)==9
                Z(i,j)=9;Z1(i,j)=9;
            end
        end
    end
    
    %Next we'll compare the predictions (Z and Z1) to the real
    %data and record the correct votes.
    inds=union(find(data0==0),find(data0==9));
    tmp=zeros(size(Z));tmp1=tmp;
    tmp(data0-Z==0)=1;
    tmp1(data0-Z1==0)=1;
    %Again, put the abstentions back in correctly
    tmp(inds)=0;tmp1(inds)=0;
    %Count the number of correct votes.
    d1=sum(tmp);d2=sum(tmp1);
    %Find the max over the two vectors - the entry that reaches
    %the max indicates which of the two predictions (Z or Z1)
    %is more accurate for that vote.
    m=max(d1,d2);
    %Construct the complete prediction matrix by making choices
    %that maximize the number of correct votes.
    for i=1:numel(m)
        if d1(i)==m(i)
            Z2(:,i)=Z(:,i);
        else
            Z2(:,i)=Z1(:,i);
        end
    end
    
    %Now, record the correct predictions
    H=zeros(size(Z));
    H(Z2-data0==0)=1;
    H(inds)=0;
    
    %Next, record the percentage correct for the held out votes
    oosa(j1)=sum(H(hinds0))/numel(hinds0);
    
    
end
oos=mean(oosa(sample));

